﻿using Imaginecup.stage;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.gameState.malaria.stageMan;
using Imaginecup.scene;
using Imaginecup.gameState.malaria.manager;
using Imaginecup.util;
using Microsoft.Xna.Framework.Input;

namespace Imaginecup.stage
{
    public abstract class BaseStage
    {
        //BaseStage에 꼭 필요하고 어느 자식에나 필수적으로 동일한 부분
        protected IStageManController m_MalariaStageMan;
        private bool m_EndStage = false;

        public void SetStageMan(IStageManController stageMan)
        {
            m_MalariaStageMan = stageMan;
        }
        
        public void SetGameOver()
        {
        //    m_MalariaStageMan.SetGameOver(); 게임오버 표시 안함.
            LifeMan.GetLifeMan().SetLife(1);
            m_MalariaStageMan.ChangeStage(StageEnum.ChoiceStage);
            m_EndStage = true;
        }
        public void SetClear()
        {
            m_MalariaStageMan.SetClear();
            m_EndStage = true;
        }

        public void ChangeStage(StageEnum stageEnum)
        {
            m_MalariaStageMan.SetEnableChoice(stageEnum);
            m_MalariaStageMan.ChangeStage(StageEnum.ChoiceStage);
            m_EndStage = true;
        }

        public virtual void Release()
        {
            UnloadResource();
        }

        protected virtual void UnloadResource()
        {
            if( m_SceneMan != null)
                 m_SceneMan.Release();
        }

     
        //Stage에 따라 상황이 달라질 수 있는 부분.

        protected ISceneMan m_SceneMan;
        public virtual void Update(GameTime gameTime)
        {
#if WINDOWS

            //치트키
            KeyboardInputHandler keyboardInput = KeyboardInputHandler.GetInputHandler();
            if (keyboardInput.IsOneKeyDown(Keys.N))
            {
                CheatKey();
            }
            else
#endif
            m_SceneMan.Update(gameTime);

            if (m_EndStage)
                UnloadResource();

        }

#if WINDOWS
        protected abstract void CheatKey();
#endif

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (m_EndStage) return;
            m_SceneMan.Draw(gameTime, spriteBatch);
        }
        
        //protected IDrawStage m_DrawStage;
        //protected IUpdateStage m_UpdateStage;
        public abstract void CheckGameState();

    }
}
